-- 佐罗冈布奥建筑

function initZooroSlime(self, buildingInfo, dbase, pos)
    if type(dbase) ~= "table" or type(dbase["need_property_one"]) ~= "table" then
        -- 没要求，配置错误
        error("获取不到佐罗冈布奥物品配置！");
        return;
    end

    local petEvent = {103, 115};
    local chipsEvent = {104, 116};

    -- 扣除触发事件
    local function okFun(eventId)
        local bonusArgs = table.deepcopy(SlimeEventM.query(eventId, "bonus_args"));
        local cost = bonusArgs["cost"][1];
        if cost[1] ~= 1 or cost[2] == 0 then
            -- 配置错误
            error("佐罗冈布奥扣除配置错误！或玩家没有装备不执行。");
            return;
        end

        -- 获取type
        local equipType = EquipM.query(cost[2], "type");
        -- 卸下佐罗面罩
        if not DungeonActionM.go("do_unequip", 1, equipType) then
            -- 卸下失败
            return;
        end

        -- 尝试触发
        DungeonActionM.go("slime_event", pos, eventId);

        -- 建筑消失
        playBuildingDisappear(self);
    end

    local petId = SlimeEventM.query(petEvent[1], "dbase")["pet_id"];
    -- 建筑图标
    local iconPath = getHeroIconPath(petId);

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 格子失效了
                return;
            end

            if not DungeonBossM.canPickUpTarget() then
                -- 不能解救
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();
            local costItem = hasEquipZorroMask(dbase["need_property_one"]);

            -- 判断是否有需求之一
            if costItem ~= 0 then
                -- 装备四星佐罗面罩
                -- 有佐罗冈布奥显示购买碎片
                if not petId then
                    error("SLIME_EVENT_105 (pet_id)没有配置！");
                    return;
                end

                -- 点击后的处理
                local function clickFun()
                    if PetM.getMyPet(petId) then
                        eventIds = chipsEvent
                    else
                        eventIds = petEvent;
                    end
                    for _, eventId in pairs(eventIds) do
                        local bonusArgs = table.deepcopy(SlimeEventM.query(eventId, "bonus_args"));
                        local cost = bonusArgs["cost"][1];
                        if costItem == cost[2] then
                            okFun(eventId)
                            break;
                        end
                    end
                end

                self:openBuildingView2(clickFun, nil, buildingInfo["view_text"],
                    buildingInfo["view_text2"], buildingInfo["dialog1"], iconPath, true);
            else
                -- 是否装备了1~3星面罩
                local zooroMaskClassIds = table.append(FormulaM.invoke("ZOORO_MASK_CLASS_IDS"),
                    FormulaM.invoke("ZOORO_MASK_CLASS_IDS2"));
                local hasZooroMask = false;

                for _, classId in pairs(zooroMaskClassIds) do
                    if EquipM.isEquiped(classId) then
                        hasZooroMask = true;
                        break;
                    end
                end

                if hasZooroMask then
                    -- 有面罩
                    self:openBuildingView(nil, 0, getLocStr("zorro_slime_dialog"), buildingInfo["view_text2"], nil, iconPath, nil, true);
                else
                    -- 没有面罩
                    self:openBuildingView(nil, 0, buildingInfo["dialog2"], buildingInfo["view_text2"], nil, iconPath, nil, true);
                end
            end
            AudioM.playFx("slime_swordman_app");
        end
    end

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
    self.icon:addTouchEventListener(onClicked);
    self.needAnimation =  true;
    self.icon:setPositionY(self.icon:getPositionY() + 15);

    -- 需要用监狱图标遮挡
    local prisonIcon = getLevelItemIconPath("prison");
    local prisonImg  = ccui.ImageView:create(prisonIcon);
    prisonImg:setTouchEnabled(false);
    prisonImg:setName("prison_icon");
    self.item:addChild(prisonImg);
end

-- 是否装备了需求的佐罗面罩
function hasEquipZorroMask(need_propertys)
    local retId = 0;
    for _, classId in pairs(need_propertys) do
        if (EquipM.isEquiped(classId)) then
            retId = classId;
            break;
        end
    end
    return retId;
end
